﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_popups
{
    public partial class Removetaak : Form
    {
        string taak = "";
        int projectID;

        public Removetaak()
        {
            
        }

        public Removetaak(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getTaken();
        }

        public void getTaken()
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand takenGet = new MySqlCommand("SELECT TaakID, Taak, Status FROM taken WHERE ProjectID='" + projectID + "'", grabConnection);

                //takenGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();

                MySqlDataReader reader = takenGet.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        takenGet.Dispose();
                    }
                    else
                    {
                        comboBox1.Items.Clear();

                        while (reader.Read())
                        {
                            comboBox1.Items.Add(reader["Taak"].ToString());
                        }

                        reader.Close();
                    }

                    grabConnection.Close();
                    reader.Dispose();
                    takenGet.Dispose();
                }
            }

            catch (MySqlException)
            {
                MessageBox.Show("Sql error detected!");
            }
        }

        public void deleteTask()
        {
            MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
            MySqlCommand deltaak = new MySqlCommand("DELETE FROM taken WHERE Taak ='" + taak + "'", grabConnection);

            try
            {
                grabConnection.Open();
                deltaak.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                MessageBox.Show("Taak succesvol verwijderd!");
                this.Close();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (taak != "")
            {
                MessageBox.Show("Er is geen taak geselecteerd");
            }
            else
            {
                taak = comboBox1.SelectedItem.ToString();
                deleteTask();
            }
        }
    }
}
